﻿using BootstrapBlazor.Components;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Application.Extensions
{
    public static class FreeSqlExtensions
    {
        #region 旧的代码,有Bug,查询会出错
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="option"></param>
        ///// <returns></returns>
        //public static DynamicFilterInfo ToDynamicFilter(this QueryPageOptions option)
        //{
        //    var ret = new DynamicFilterInfo() { Filters = new() };

        //    // 处理模糊搜索
        //    if (option.Searches.Any())
        //    {
        //        ret.Filters.Add(new()
        //        {
        //            Logic = DynamicFilterLogic.Or,
        //            Filters = option.Searches.Select(i => i.ToDynamicFilter()).ToList()
        //        });
        //    }

        //    // 处理自定义搜索
        //    if (option.CustomerSearches.Any())
        //    {
        //        ret.Filters.AddRange(option.CustomerSearches.Select(i => i.ToDynamicFilter()));
        //    }

        //    // 处理高级搜索
        //    if (option.AdvanceSearches.Any())
        //    {
        //        ret.Filters.AddRange(option.AdvanceSearches.Select(i => i.ToDynamicFilter()));
        //    }

        //    // 处理表格过滤条件
        //    if (option.Filters.Any())
        //    {
        //        ret.Filters.AddRange(option.Filters.Select(i => i.ToDynamicFilter()));
        //    }
        //    return ret;
        //}

        //private static DynamicFilterInfo ToDynamicFilter(this IFilterAction filter)
        //{
        //    var actions = filter.GetFilterConditions();
        //    var item = new DynamicFilterInfo();

        //    if (actions.Filters != null)
        //    {
        //        // TableFilter 最多仅两个条件
        //        if (actions.Filters.Count == 2)
        //        {
        //            item.Logic = actions.FilterLogic.ToDynamicFilterLogic();
        //            item.Filters = actions.Filters.Select(i => new DynamicFilterInfo()
        //            {
        //                Field = i.FieldKey,
        //                Value = i.FieldValue,
        //                Operator = i.FilterAction.ToDynamicFilterOperator()
        //            }).ToList();
        //        }
        //        else
        //        {
        //            var c = actions.Filters.First();
        //            item.Field = c.FieldKey;
        //            item.Value = c.FieldValue;
        //            item.Operator = c.FilterAction.ToDynamicFilterOperator();
        //        }
        //    }
        //    return item;
        //}

        //private static DynamicFilterLogic ToDynamicFilterLogic(this FilterLogic logic) => logic switch
        //{
        //    FilterLogic.And => DynamicFilterLogic.And,
        //    _ => DynamicFilterLogic.Or
        //};

        //private static DynamicFilterOperator ToDynamicFilterOperator(this FilterAction action) => action switch
        //{
        //    FilterAction.Equal => DynamicFilterOperator.Equal,
        //    FilterAction.NotEqual => DynamicFilterOperator.NotEqual,
        //    FilterAction.Contains => DynamicFilterOperator.Contains,
        //    FilterAction.NotContains => DynamicFilterOperator.NotContains,
        //    FilterAction.GreaterThan => DynamicFilterOperator.GreaterThan,
        //    FilterAction.GreaterThanOrEqual => DynamicFilterOperator.GreaterThanOrEqual,
        //    FilterAction.LessThan => DynamicFilterOperator.LessThan,
        //    FilterAction.LessThanOrEqual => DynamicFilterOperator.LessThanOrEqual,
        //    _ => throw new System.NotSupportedException()
        //};
        #endregion

        //
        // 摘要:
        //     QueryPageOptions 转化为 FreeSql ORM DynamicFilterInfo 类型扩展方法
        //
        // 参数:
        //   option:
        //
        //   dynamicFilterInfoConverter:
        //     当 BootstrapBlazor.Components.FilterAction 的枚举值为 BootstrapBlazor.Components.FilterAction.CustomPredicate
        //     时的自定义转换方法。
        public static DynamicFilterInfo ToDynamicFilter(this QueryPageOptions option, Func<FilterKeyValueAction, DynamicFilterInfo>? dynamicFilterInfoConverter = null)
        {
            Func<FilterKeyValueAction, DynamicFilterInfo> dynamicFilterInfoConverter2 = dynamicFilterInfoConverter;
            DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo
            {
                Filters = new List<DynamicFilterInfo>()
            };
            if (option.Searches.Count > 0)
            {
                dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                {
                    Logic = DynamicFilterLogic.Or,
                    Filters = option.Searches.Select((IFilterAction i) => i.ToDynamicFilter(dynamicFilterInfoConverter2)).ToList()
                });
            }

            if (option.CustomerSearches.Count > 0)
            {
                dynamicFilterInfo.Filters.AddRange(option.CustomerSearches.Select((IFilterAction i) => i.ToDynamicFilter(dynamicFilterInfoConverter2)));
            }

            if (option.AdvanceSearches.Count > 0)
            {
                dynamicFilterInfo.Filters.AddRange(option.AdvanceSearches.Select((IFilterAction i) => i.ToDynamicFilter(dynamicFilterInfoConverter2)));
            }

            if (option.Filters.Count > 0)
            {
                dynamicFilterInfo.Filters.AddRange(option.Filters.Select((IFilterAction i) => i.ToDynamicFilter(dynamicFilterInfoConverter2)));
            }

            return dynamicFilterInfo;
        }

        //
        // 摘要:
        //     IFilterAction 转化为 DynamicFilterInfo 扩展方法
        //
        // 参数:
        //   filter:
        //
        //   dynamicFilterInfoConverter:
        //     当 BootstrapBlazor.Components.FilterAction 的枚举值为 BootstrapBlazor.Components.FilterAction.CustomPredicate
        //     时的自定义转换方法。
        public static DynamicFilterInfo ToDynamicFilter(this IFilterAction filter, Func<FilterKeyValueAction, DynamicFilterInfo>? dynamicFilterInfoConverter = null)
        {
            return filter.GetFilterConditions().ParseDynamicFilterInfo(dynamicFilterInfoConverter);
        }

        private static DynamicFilterInfo ParseDynamicFilterInfo(this FilterKeyValueAction filterKeyValueAction, Func<FilterKeyValueAction, DynamicFilterInfo>? dynamicFilterInfoConverter = null)
        {
            Func<FilterKeyValueAction, DynamicFilterInfo> dynamicFilterInfoConverter2 = dynamicFilterInfoConverter;
            if (filterKeyValueAction.FilterAction != FilterAction.CustomPredicate)
            {
                return new DynamicFilterInfo
                {
                    Operator = filterKeyValueAction.FilterAction.ToDynamicFilterOperator(),
                    Logic = filterKeyValueAction.FilterLogic.ToDynamicFilterLogic(),
                    Field = filterKeyValueAction.FieldKey,
                    Value = filterKeyValueAction.FieldValue,
                    Filters = filterKeyValueAction.Filters?.Select((FilterKeyValueAction i) => i.ParseDynamicFilterInfo(dynamicFilterInfoConverter2)).ToList()
                };
            }

            return dynamicFilterInfoConverter2?.Invoke(filterKeyValueAction) ?? throw new InvalidOperationException("The parameter dynamicFilterInfoConverter can't not null");
        }

        private static DynamicFilterLogic ToDynamicFilterLogic(this FilterLogic logic)
        {
            if (logic == FilterLogic.And)
            {
                return DynamicFilterLogic.And;
            }

            return DynamicFilterLogic.Or;
        }

        private static DynamicFilterOperator ToDynamicFilterOperator(this FilterAction action)
        {
            return action switch
            {
                FilterAction.Equal => DynamicFilterOperator.Equal,
                FilterAction.NotEqual => DynamicFilterOperator.NotEqual,
                FilterAction.Contains => DynamicFilterOperator.Contains,
                FilterAction.NotContains => DynamicFilterOperator.NotContains,
                FilterAction.GreaterThan => DynamicFilterOperator.GreaterThan,
                FilterAction.GreaterThanOrEqual => DynamicFilterOperator.GreaterThanOrEqual,
                FilterAction.LessThan => DynamicFilterOperator.LessThan,
                FilterAction.LessThanOrEqual => DynamicFilterOperator.LessThanOrEqual,
                _ => throw new NotSupportedException("Please use the ToDynamicFilter method second parameter to support CustomPredicate"),
            };
        }
    }
}
